﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GiaiPTBac2
{
    public partial class GiaiPTBac2 : Form
    {
        double a, b, c, n, n1, n2, delta;
        void tinh(double a, double b, double c)
        {
            if (a == 0)
            {
                MessageBox.Show("day khong fai la phuong trinh bac 2");
                if (b == 0)
                {
                    txtDelta.Text = "";
                    txtKQ1.Text = "pt vo nghiem";
                    txtKQ2.Text = "pt vo nghiem";
                }
                else
                {
                    n1 = n2 = (-c) / b;
                    txtKQ1.Text = n1.ToString();
                    txtKQ2.Text = n2.ToString();
                }
            }
            else
            {
                delta = b * b - 4 * a * c;
                if (delta < 0)
                {
                    txtDelta.Text=" < 0 ";
                    txtKQ1.Text = "pt vo nghiem";
                    txtKQ2.Text = "pt vo nghiem";
                }
                else 
                {
                    txtDelta.Text=delta.ToString();
                    n1 = ((-b) + Math.Sqrt(delta)) / (2 * a);
                    n2 = ((-b) - Math.Sqrt(delta)) / (2 * a);
                    txtKQ1.Text = n1.ToString();
                    txtKQ2.Text = n2.ToString();
                }
            }
        }
        void xoa()
        {
            a = 0;
            b = 0;
            c = 0;
            n1 = 0;
            n2 = 0;
            delta = 0;
            txtHSa.Text = "";
            txtHSb.Text = "";
            txtHSc.Text = "";
            txtKQ1.Text = "";

        }
        public GiaiPTBac2()
        {
            InitializeComponent();
            xoa();
        }

        private void btnGiai_Click(object sender, EventArgs e)
        {
            a = double.Parse(txtHSa.Text);
            b = double.Parse(txtHSb.Text);
            c = double.Parse(txtHSc.Text);
            tinh(a, b, c);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            xoa();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            TextBox A = (TextBox)sender;
            double a;
            try
            {
                if (A.Text == "-")
                {
                    return;
                }
                a = double.Parse(A.Text);
            }
            catch (Exception)
            {
                A.Text = "";
            }
        }
    }
}
